package org.NooLab.nativebrowser.app.controller.parser;

import java.util.ArrayList;

import org.NooLab.utilities.strings.ArrUtilities;
import org.NooLab.utilities.strings.StringsUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


/**
 * 
 * getting all div
 * 
 * important for deciding which content we will extract
 * 
 *
 */
public class DivExtractor {

	
	Document soupdoc;
	
	ArrayList<String> divList = new ArrayList<String>();
	ArrayList<Integer> divpositions = new ArrayList<Integer>();
	
	
	private String divtext="", html ="";
	private String containedText="";

	StringsUtil strgutil = new StringsUtil();
	
	// ========================================================================
	public DivExtractor( HTMLParser htmlParser, Document soupdoc ){
		
		html = htmlParser.htmlStr ;
		this.soupdoc = soupdoc;
	}
	// ========================================================================
	
	
	public int getLength(){
		return 0;
	}
	
	public String getText(){
		return "";
	}
	
	public String getId(){
		return "";
	}
	
	/**
	 * this creates two lists:
	 *  - using jsoup
	 *  - just the div openings,---> getting the id,class, role
	 *  
	 * @return
	 */
	public DivExtractor parse(){
		int p,pc,pn;
		String ds;
		
		Elements contentDivs ;
		Element  contentDiv; 
		
		
		
		contentDivs = soupdoc.select("div"); // [id=content] .first()
		
		for (int i=0;i<contentDivs.size();i++){
		
			contentDiv = contentDivs.get(i) ;
			divtext = contentDiv.toString(); // difference between those ??
			containedText = contentDiv.text();
			
		}
		 
				
		
		int[] dpos = strgutil.indexesOfStrings(html, new String[]{"<div"}, 0, true);
		ArrayList<Integer> divpositions;
		
		divpositions = ArrUtilities.changeArraystyle(dpos) ;
		 
		for (int i=0;i<divpositions.size();i++){
			
			p = divpositions.get(i) ;
			pc = html.indexOf(">", p+1);
			pn = html.indexOf("<", p+2);
			
			if ((p>0) && (pc>p) && (pc-p<40) && (pc<pn)){
				ds = html.substring(p,pc+1) ;
				divList.add(ds);
			}else{
				divList.add("");
			}
			
			
		}// i->

		return this;
	}
	
	
	
	
}
